package com.hzyl.famousreader.util;

import android.content.ContentResolver;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.view.Window;
import android.view.WindowManager;
import com.blankj.utilcode.util.Utils;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/02/08
 *     desc  : 亮度相关工具类
 * </pre>
 */
public final class BrightnessUtils {

  private BrightnessUtils() {
    throw new UnsupportedOperationException("u can't instantiate me...");
  }

  /**
   * 判断是否开启自动调节亮度
   *
   * @return {@code true}: 是<br>{@code false}: 否
   */
  public static boolean isAutoBrightnessEnabled() {
    try {
      int mode = Settings.System.getInt(
        Utils.getApp().getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS_MODE
      );
      return mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
    } catch (Settings.SettingNotFoundException e) {
      e.printStackTrace();
      return false;
    }
  }

  /**
   * 设置是否开启自动调节亮度
   * <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
   * 并得到授权
   *
   * @param enabled {@code true}: 打开<br>{@code false}: 关闭
   * @return {@code true}: 成功<br>{@code false}: 失败
   */
  public static boolean setAutoBrightnessEnabled(final boolean enabled) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
      && !Settings.System.canWrite(Utils.getApp())) {
      Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
      intent.setData(Uri.parse("package:" + Utils.getApp().getPackageName()));
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      Utils.getApp().startActivity(intent);
      return false;
    }
    return Settings.System.putInt(
      Utils.getApp().getContentResolver(),
      Settings.System.SCREEN_BRIGHTNESS_MODE,
      enabled ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC
        : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
    );
  }

  /**
   * 获取屏幕亮度
   *
   * @return 屏幕亮度 0-255
   */
  public static int getBrightness() {
    try {
      return Settings.System.getInt(
        Utils.getApp().getContentResolver(),
        Settings.System.SCREEN_BRIGHTNESS
      );
    } catch (Settings.SettingNotFoundException e) {
      e.printStackTrace();
      return 0;
    }
  }

  /**
   * 设置屏幕亮度
   * <p>需添加权限 {@code <uses-permission android:name="android.permission.WRITE_SETTINGS" />}</p>
   * 并得到授权
   *
   * @param brightness 亮度值
   */
  public static boolean setBrightness(@IntRange(from = 0, to = 255) final int brightness) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
      && !Settings.System.canWrite(Utils.getApp())) {
      Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
      intent.setData(Uri.parse("package:" + Utils.getApp().getPackageName()));
      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      Utils.getApp().startActivity(intent);
      return false;
    }
    ContentResolver resolver = Utils.getApp().getContentResolver();
    boolean b = Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
    resolver.notifyChange(Settings.System.getUriFor("screen_brightness"), null);
    return b;
  }

  /**
   * 设置窗口亮度
   *
   * @param window     窗口
   * @param brightness 亮度值
   */
  public static void setWindowBrightness(@NonNull final Window window,
                                         @IntRange(from = 0, to = 255) final int brightness) {
    WindowManager.LayoutParams lp = window.getAttributes();
    lp.screenBrightness = brightness / 255f;
    window.setAttributes(lp);
  }

  /**
   * 获取窗口亮度
   *
   * @param window 窗口
   * @return 屏幕亮度 0-255
   */
  public static int getWindowBrightness(final Window window) {
    WindowManager.LayoutParams lp = window.getAttributes();
    float brightness = lp.screenBrightness;
    if (brightness < 0) return getBrightness();
    return (int) (brightness * 255);
  }
}
